#include <stdio.h>
#include <stdlib.h>
//
// Created by win10 on 2024/11/29.
//
typedef struct {
    int data;
    struct Ltree * lchild;
    struct Ltree * rchild;
}Ltree;

// 2. 初始化
Ltree * createLtree(int value) {
    //为子节点分配空间
    Ltree * newNode = (Ltree *) malloc(sizeof(Ltree));
    //鲁棒性判断
    if(newNode == NULL) {
        return NULL;
    }
    newNode->data = value;
    newNode->lchild = NULL;
    newNode->rchild = NULL;
    return newNode;
}
//先序遍历
void preorder(Ltree *T) {
    if (T == NULL) {
        return;
    }
    printf("%c ", T->data);
    preorder(T->lchild);
    preorder(T->rchild);
}
//中序遍历
void inorder(Ltree *T) {
    if (T == NULL) {
        return;
    }
    inorder(T->lchild);
    //打印无左子树的节点或已遍历左子树的节点
    printf("%c ", T->data);
    //最后遍历
    inorder(T->rchild);
}
int main(void) {
    Ltree * nodeA = createLtree('A');
    Ltree * nodeB = createLtree('B');
    Ltree * nodeC = createLtree('C');
    Ltree * nodeD = createLtree('D');
    Ltree * nodeE = createLtree('F');
    //节点的连接
    nodeA->lchild = nodeB;
    nodeA->rchild = nodeC;
    nodeB->lchild = nodeD;
    nodeB->rchild = nodeE;
    preorder(nodeA);
}

